<?php
/**
 * Validating data
 */
 class Validate{
	 
	/**
	 * Dates validation.
	 * 
	 * @param string $date "dd/mm/aaaa"
	 */
	public static function date($date){

		$ok = false;
	  	//pattern: dd/mm/aaaa
	  	$regexp = "^([[:digit:]]{1,2})/([[:digit:]]{1,2})/([[:digit:]]{2,4})$";
	  	
	  	if(!eregi($regexp, $data))
	  		return false;
	  	else{
	  		//comprobamos los rangos de los números
	  		$date = array();
	  		$date = explode("/", $data);
	  		$day = $date[0];
	  		$month = $date[1];
	  		$year = $date[2];
	  	
	  		if($month<1 || $month>12 || $day<1)
	  			return false;
	  		
	  		switch($month){
	  			case 1:
	  			case 3:
	  			case 5:
	  			case 7:
	  			case 8:
	  			case 10:
	  			case 12:
	  				if($day <= 31)
	  					$ok = true;
	  				break;
	  			case 2:
	  				//leap-year
	  				$leap_year = false;
	  					  				
	  				$r4 = fmod($year, 4);
	  				$r100 = fmod($year, 100);
	  				$r400 = fmod($year, 400);
	  				
	  				if($r4 != 0)
	  					$leap_year = false;
	  				else if($r100 != 0)
	  					$leap_year = true;
	  				else if($r100 == 0 && $r400 != 0)
	  					$leap_year = false;
	  				else if($r100 == 0 && $r400 == 0)
	  					$leap_year = true;

	  				if($leap_year){
	  					if($day<=29)
	  						$ok = true;
	  				}
	  	  			else if($day<=28){
	  						$ok = true;
	  	  			}
	  				break;
	  			case 4:
	  			case 6:
	  			case 9:
	  			case 11:
	  				if($day<=30)
	  					$ok = true;
	  				break;
	  		}
	  	}
	  		
	  	return $ok;
	}
	  
 	/**
	 * Generic id (Integer no longer 11 digits)
	 * 
	 * @param integer $data 
	 */
	public static function id($data){
		$ok = false;
		//8 dígitos máximo, 2 como mínimo
		$regexp = "^[[:digit:]]{1,11}$";
		 	
		if(is_numeric($data) && eregi($regexp, $data))
			$ok = true;
		 		
		return $ok;
	 }
	  
 	/**
 	 * Strings
	 * 
	 * @param string $data 
 	 */
 	public static function string($data){
 		$ok = false;
 		
 		//characters allowed
 		$regexp = "[^[:alnum:]áéíóúÁÉÍÓÚ _.,:;ñÑºª@ü()'\"/-]+<>";
 		
	 	if(!eregi($regexp, $data))
	  		$ok = true;
	
	  	if($ok==false)
	  		$this->error=true;
	  		
		return $ok;
 	}
 	 	
 	/*
	 * Email
	 * 
	 * @param string $email
	 */
	public static function email($email){
		if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) 
			return true;
		else
			return false;
	
	}	
} 
?>
